package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.EmployeeInfoMapper;
import com.ruoyi.business.domain.EmployeeInfo;
import com.ruoyi.business.service.IEmployeeInfoService;

import javax.validation.Validator;

/**
 * 员工信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-17
 */
@Service
public class EmployeeInfoServiceImpl  extends BaseServiceImpl implements IEmployeeInfoService
{
    @Autowired
    private EmployeeInfoMapper employeeInfoMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询员工信息
     * 
     * @param id 员工信息主键
     * @return 员工信息
     */
    @Override
    public EmployeeInfo selectEmployeeInfoById(Long id)
    {
        return employeeInfoMapper.selectEmployeeInfoById(id);
    }

    /**
     * 查询员工信息列表
     * 
     * @param employeeInfo 员工信息
     * @return 员工信息
     */
    @Override
    public List<EmployeeInfo> selectEmployeeInfoList(EmployeeInfo employeeInfo)
    {
        return employeeInfoMapper.selectEmployeeInfoList(employeeInfo);
    }

    /**
     * 新增员工信息
     * 
     * @param employeeInfo 员工信息
     * @return 结果
     */
    @Override
    public int insertEmployeeInfo(EmployeeInfo employeeInfo)
    {
        employeeInfo.setCreateTime(DateUtils.getNowDate());
        return employeeInfoMapper.insertEmployeeInfo(employeeInfo);
    }

    /**
     * 修改员工信息
     * 
     * @param employeeInfo 员工信息
     * @return 结果
     */
    @Override
    public int updateEmployeeInfo(EmployeeInfo employeeInfo)
    {
        employeeInfo.setUpdateTime(DateUtils.getNowDate());
        return employeeInfoMapper.updateEmployeeInfo(employeeInfo);
    }

    /**
     * 批量删除员工信息
     * 
     * @param ids 需要删除的员工信息主键
     * @return 结果
     */
    @Override
    public int deleteEmployeeInfoByIds(Long[] ids)
    {
        return employeeInfoMapper.deleteEmployeeInfoByIds(ids);
    }

    /**
     * 删除员工信息信息
     * 
     * @param id 员工信息主键
     * @return 结果
     */
    @Override
    public int deleteEmployeeInfoById(Long id)
    {
        return employeeInfoMapper.deleteEmployeeInfoById(id);
    }

    @Override
    public String importData(List<EmployeeInfo> userList, boolean updateSupport, String operName) {
    if (userList == null || userList.isEmpty()) {
        throw new ServiceException("导入员工信息数据不能为空！");
    }

    int successNum = 0;
    int failureNum = 0;
    StringBuilder successMsg = new StringBuilder();
    StringBuilder failureMsg = new StringBuilder();
        List<String>failureNums = new ArrayList<>();
    int i = 1;
    for (EmployeeInfo user : userList) {
        try {
            i++;
            if (user.getEmployeeId() ==null || user.getEmployeeId().equals("")) {
                failureNums.add(String.valueOf(i));
                failureNum++;
                continue ;
            }
            // 验证是否存在这个员工信息
            EmployeeInfo employeeInfo = new EmployeeInfo();
            employeeInfo.setEmployeeId(user.getEmployeeId());

            List<EmployeeInfo> selectEmployeeInfoList = employeeInfoMapper.selectEmployeeInfoList(employeeInfo);


            if (selectEmployeeInfoList.isEmpty()) {
                // 验证数据
                BeanValidators.validateWithException(validator, user);
                // 设置初始值
                user.setCreateTime(DateUtils.getNowDate());
                user.setCreateBy(operName);
                employeeInfoMapper.insertEmployeeInfo(user);
                successNum++;
                //successMsg.append("<br/>" + successNum + "、员工信息 " + user.getName() + " 导入成功");
            } else if (true) {
                // 验证数据
                EmployeeInfo existingEmployee = selectEmployeeInfoList.get(0);
                BeanValidators.validateWithException(validator, user);
                user.setId(existingEmployee.getId());
                user.setUpdateTime(DateUtils.getNowDate());
                user.setUpdateBy(operName);
                employeeInfoMapper.updateEmployeeInfo(user);
                successNum++;
                //successMsg.append("<br/>" + successNum + "、员工信息 " + user.getName() + " 更新成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、员工信息 " + user.getName() + " 已存在");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、员工信息 " + user.getName() + " 导入失败：";
            failureMsg.append(msg + e.getMessage());
        }
    }

    if (failureNum > 0) {
        failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确");
        //throw new ServiceException(failureMsg.toString());
    } else {
        successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
    }

        setCacheInfo("员工信息",String.valueOf(successNum),String.valueOf(failureNum),failureNums.stream().collect(Collectors.joining(",")));

    return successMsg.toString() + failureMsg.toString();
}

}
